package com.zzxx.userManagement.dao.impl;

import com.zzxx.userManagement.dao.UserInfoDao;
import com.zzxx.userManagement.domain.UserInfo;
import com.zzxx.userManagement.utils.DruidUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class UserInfoDaoImpl implements UserInfoDao {
    private JdbcTemplate jdbcTemplate = new JdbcTemplate(DruidUtils.getDataSource());
    @Override
    public List<UserInfo> findAll() {
        String sql = "select * from userInfo";

        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(UserInfo.class));
    }

    @Override
    public void deleteById(Integer id) {
        String sql = "delete from userInfo where id = ?";
        jdbcTemplate.update(sql, id);
    }

    @Override
    public UserInfo findById(Integer id) {
        String sql = "select * from userInfo where id = ?";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(UserInfo.class), id);
    }

    @Override
    public void update(UserInfo userInfo) {
        String sql = "update userInfo set gender=?,address=?,email=?,qq=?,age=? where id=?";
        jdbcTemplate.update(sql, userInfo.getGender(), userInfo.getAddress(), userInfo.getEmail(),userInfo.getQq(),userInfo.getAge(),userInfo.getId());
    }

    @Override
    public void add(UserInfo userInfo) {
        String sql = "insert into userInfo(name,age,gender,address,email,qq) values (?,?,?,?,?,?)";
        jdbcTemplate.update(sql, userInfo.getName(),userInfo.getAge(),userInfo.getGender(), userInfo.getAddress(), userInfo.getEmail(),userInfo.getQq());

    }

    @Override
    public List<UserInfo> findByPage(int start, int pageSize, Map<String, String[]> condition) {
        StringBuilder _sql = new StringBuilder("select * from userInfo where 1=1 ");
        List<Object> params = new ArrayList<>();
        Set<String> keys = condition.keySet();
        for (String key : keys) {
            // address name email
            if (key.equals("name")) {
                _sql.append(" and name like ? ");
                // 添加参数
                params.add("%" + condition.get(key)[0] + "%");
            } else if (key.equals("address")){
                _sql.append(" and address like ? ");
                params.add("%" + condition.get(key)[0] + "%");
            } else if (key.equals("email")) {
                _sql.append(" and email like ? ");
                params.add("%" + condition.get(key)[0] + "%");
            }
        }
        _sql.append(" limit ?,?");
        params.add(start);
        params.add(pageSize);
        return jdbcTemplate.query(_sql.toString(), params.toArray(), new BeanPropertyRowMapper<>(UserInfo.class));
    }

    @Override
    public int findCount(Map<String, String[]> condition) {
        StringBuilder _sql = new StringBuilder("select count(*) from userInfo where 1=1 ");
        List<Object> params = new ArrayList<>();
        Set<String> keys = condition.keySet();
        for (String key : keys) {
            // address name email
            if (key.equals("name")) {
                _sql.append(" and name like ? ");
                // 添加参数
                params.add("%" + condition.get(key)[0] + "%");
            } else if (key.equals("address")){
                _sql.append(" and address like ? ");
                params.add("%" + condition.get(key)[0] + "%");
            } else if (key.equals("email")) {
                _sql.append(" and email like ? ");
                params.add("%" + condition.get(key)[0] + "%");
            }
        }
        return jdbcTemplate.queryForObject(_sql.toString(), params.toArray(), Integer.class);
    }
}
